package com.heima.media.task;

import com.heima.media.entity.WmNews;
import com.heima.media.service.IAuditService;
import com.heima.media.service.IWmNewsService;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class ArticleTask {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private IWmNewsService wmNewsService;

    @Autowired
    private IAuditService auditService;

    @Async
    public void receiveMesasge() throws Exception{
        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque("article-bqueue");
        while (true) {
            Object poll = blockingDeque.poll(30, TimeUnit.SECONDS);
            if(poll!=null){
              Integer newsId = Integer.valueOf(poll.toString());
                //            获取到消息后发布文章
                WmNews wmNews = wmNewsService.getById(newsId);
                Long articleId =   auditService.saveArticle(wmNews);//把auditService中方法做出public 并且在接口中添加
                wmNews.setArticleId(articleId);
                wmNews.setStatus(9);
                wmNewsService.updateById(wmNews);
            }
        }
    }
}
